package XML与JSON.使用JACKSON;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import lombok.Data;
import lombok.ToString;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*
•Jackson解析xml
•Jackson解析xml并不常用，推荐使用Dom4j

导入jar文件：
//基本
jackson-annotations-2.9.9.jar
jackson-core-2.9.9.jar
jackson-databind-2.9.9.jar
//支持XML
jackson-dataformat-xml-2.9.8.jar
jackson-module-jaxb-annotations-2.9.8.jar
stax2-api-3.1.4.jar
一、Java对象转xml
POJO类：
*/
@Data
@JacksonXmlRootElement(localName = "Students")
@ToString
class Student {
	
	@JacksonXmlProperty(isAttribute=true,localName = "id")
    private String id;
    //@JacksonXmlProperty(localName = "name")
    private String name;
    private String gender;
    private String phone;
 
    public Student() {}
    public Student(String id, String name, String gender, String phone) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.phone = phone;
    }
 
    //省略getter和setter方法      已使用lombok @Data注解
 
//    @Override
//    public String toString() {
//        return "Student{" +
//                "id='" + id + '\'' +
//                ", name='" + name + '\'' +
//                ", gender='" + gender + '\'' +
//                ", phone='" + phone + '\'' +
//                '}';
//    }
}
/*
Java对象在转XML时，是根据POJO对象中的getter方法，获取POJO中的字段值，如果某个字段没有getter方法，将不能被解析为XML。另外，还POJO类中需要包括一个所
有拥有getter方法的字段的构造器，否则可能导致某个字段序列化失败。

1、POJO转XML格式的String字符串
*/

public class jackson解析xml {
    @Test
    public void test1(){
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
        //xmlMapper.setDefaultUseWrapper(true);
        xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
        List<Student> students= new ArrayList<>();
        Student student1 = new Student("001","Jack","男","12343464646");
        students.add(student1);
        Student student2 = new Student("002","Jack","男","12343464646");
        students.add(student2);
        Student student3 = new Student("003","Jack","男","12343464646");
        students.add(student3);
        try {
            String strXML = xmlMapper.writeValueAsString(students);
            System.out.println(strXML);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

/*
结果：
<Student xmlns=""><id>004</id><name>Jack</name><gender>男</gender><phone>12343464646</phone></Student>

2、POJO转成XML文件
*/
/*
public class jackson解析xml {
    @Test
    public void test2(){
        XmlMapper xmlMapper = new XmlMapper();
        //@JacksonXmlElementWrapper 用于指定List等集合类，外围标签名；用在非集合类上无效。一般用于list，list外层的标签。若不用的话，useWrapping =false。
        //xmlMapper.setDefaultUseWrapper(false);
        Student student = new Student("004","Jack","男","12343464646");
        try {
            xmlMapper.writeValue(new File("src\\main\\java\\XML与JSON\\使用JACKSON\\jack.xml"),student);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/*
/*
XML文件：

<Student xmlns="">
    <id>004</id>
    <name>Jack</name>
    <gender>男</gender>
    <phone>12343464646</phone>
</Student>
3、List转XML文件
*/
/*
public class jackson解析xml {  //List转XML文件
	@JacksonXmlElementWrapper(localName = "students")  //localName:不包括命名空间名
	List<Student> students = new ArrayList<>();
    @Test
    public void test3(){
   	 XmlMapper xmlMapper = new XmlMapper();
   	 xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);//将xml文件美化成带缩进的换行格式
        //xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        //xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        //xmlMapper.setDefaultUseWrapper(true);
        Student student1 = new Student("004","Jack","男","12343464646");
        Student student2 = new Student("002","Tom","Male","18765466643");
        Student student3 = new Student("003","JackSon","Male","18765477743");
        students.add(student1);
        students.add(student2);
        students.add(student3);
        try {
            xmlMapper.writeValue(new File("src\\main\\java\\XML与JSON\\使用JACKSON\\students1.xml"),students);//生成xml文件
            String str=xmlMapper.writeValueAsString(students);//生成字符串
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
*/
/*
结果：

可以看出，集合中的每个POJO对象，在XML文件中默认使用<item>作为父节点。

<ArrayList xmlns="">
    <item>
        <id>004</id>
        <name>Jack</name>
        <gender>男</gender>
        <phone>12343464646</phone>
    </item>
    <item>
        <id>002</id>
        <name>Tom</name>
        <gender>Male</gender>
        <phone>18765466643</phone>
    </item>
    <item>
        <id>003</id>
        <name>JackSon</name>
        <gender>Male</gender>
        <phone>18765477743</phone>
    </item>
</ArrayList>
二、XML转Java对象
1、XML 格式的String转POJO

    @Test
    public void test4(){
        String strStudent = "<Student xmlns=\"\"><id>004</id><name>Jack</name><gender>男</gender><phone>12343464646</phone></Student>";
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setDefaultUseWrapper(false);
        try {
            Student student = xmlMapper.readValue(strStudent,Student.class);
            System.out.println(student);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
结果：

Student{id='', name='Jack', gender='男', phone='12343464646'}
2、XML文件转POJO

    @Test
    public void test5(){
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setDefaultUseWrapper(false);
        try {
            Student student = xmlMapper.readValue(new File("xml/jack.xml"),Student.class);
            System.out.println(student);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
结果：

Student{id='', name='Jack', gender='男', phone='12343464646'}
3、XML文件转List

    @Test
    public void test6(){
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setDefaultUseWrapper(false);
        List<Student> students;
        try {
            students = xmlMapper.readValue(new File("xml/students.xml"),new TypeReference<List<Student>>(){});
            for (Student student : students) {
                System.out.println(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
结果：

Student{id='004', name='Jack', gender='男', phone='12343464646'}
Student{id='002', name='Tom', gender='Male', phone='18765466643'}
Student{id='003', name='JackSon', gender='Male', phone='18765477743'}
*/